pip install matplotlib
pip install altair
pip install folium
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import altair as alt
import folium as folium
ca_covid = pd.read_csv("https://raw.githubusercontent.com/datadesk/california-coronavirus-data/master/latimes-place-totals.csv")
ca_covid.head()
# Unique County Names
county_name_list = ca_covid['county'].unique()
county_name_list = county_name_list.tolist()
county_name_list.insert(0, 'Select One')
print('Unique County Names: %d' % len(county_name_list))
county_name = 'Orange'
ca_county = ca_covid[ca_covid['county']== county_name]
ca_county = ca_county.sort_values('date', ascending = True)
ca_county.head()
city_name = 'Irvine'
ca_city = ca_county[ca_county['place']== city_name]
ca_city = ca_city.sort_values('date', ascending = True)
ca_city.head()
style.use('ggplot')
ax = plt.gca()
df_county.plot(kind='line',x='date',y='confirmed_cases', color='green', ax=ax, figsize=(20,10))
plt.title('Number of Cases In ' + county_name +' County')
plt.xlabel('date')
plt.ylabel('confirmed_cases')
plt.show()
import ipywidgets as widgets
from IPython.display import clear_output
default_county = 'Orange'
country_widget = widgets.Dropdown(
options= county_name_list,
value= default_county,
description='county:',
)
def refresh_chart(county_name):
# reset plot and widget
clear_output(wait=True)
display(country_widget)
if county_name != 'Select One':
ca_counties = pd.read_csv("https://raw.githubusercontent.com/datadesk/california-coronavirus-data/master/latimes-place-totals.csv")
ca_county = ca_counties[ca_counties['county'] == county_name]
ca_county = ca_county.sort_values('date', ascending = True)
style.use('ggplot')
ax = plt.gca()
ca_county.plot(kind='line',x='date',y='confirmed_cases', color='green', ax=ax, figsize=(20,10))
plt.title('Number of Cases In ' + county_name +' County')
plt.xlabel('date')
plt.ylabel('confirmed_cases')
plt.show()
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
refresh_chart(change['new'])
country_widget.observe(on_change)
display(country_widget)
refresh_chart(default_county)
ca_covid.head()
ca_latest_county = ca_covid[ca_covid['date'] == ca_covid['date'].max()]
del ca_latest_county['date']
del ca_latest_county['confirmed_cases']
ca_latest_county.head()
ca_county.query("confirmed_cases == 'NaN'")
ca_county.query("x == 'NaN'")
ca_county.query("y == 'NaN'")
ca_covid.query("x > 0")
ca_county.query("date.isnull()", engine='python')
ca_county = ca_county.query("confirmed_cases != 'NaN' & x < 0 & x != 'NaN' & date.notnull()", engine='python')
ca_county.head()
ca_covid = ca_covid.query("confirmed_cases != 'NaN' & x < 0 & x != 'NaN' & date.notnull()", engine='python')
ca_covid.head()
ca_city = ca_city.query("confirmed_cases != 'NaN' & x < 0 & x != 'NaN' & date.notnull()", engine='python')
ca_city.head()
import requests as r
import json
from IPython.display import HTML
ca_json = r.get('https://raw.githubusercontent.com/OpenDataDE/State-zip-code-GeoJSON/master/ca_california_zip_codes_geo.min.json')
print(ca_json)
cali_map = folium.Map(location=[33.669445,-117.823059], zoom_start=12)
folium.Choropleth(geo_data=ca_json.json()['features'][512],
fill_color='red',
fill_opacity=0.5,
line_opacity=0.8,
key_on='feature.properties.ZCTA5CE10').add_to(cali_map)
cali_map.save('plot_data_2.html')
HTML('<iframe src=plot_data_2.html width=800 height=500></iframe>')
ca_city.head()
ca_city.shape
ca_city = ca_city.sort_values(by=["date"], ascending=True)
ca_city.tail()
ca_city.iloc[0]['date']
lastdate = ca_city.iloc[-1]['date']
lastdate
irvine_single_day = ca_city.query('date==@lastdate')
irvine_single_day = irvine_single_day.sort_values(by=["confirmed_cases"], ascending=True)
irvine_single_day
orange_single_day = ca_county.query('date==@lastdate')
orange_single_day = orange_single_day.sort_values(by=["confirmed_cases"], ascending=True)
orange_single_day.head()
ca_single_day = ca_covid.query('date==@lastdate')
ca_single_day
ca_single_day = ca_single_day.query("confirmed_cases != 'NaN' & x < 0 & x != 'NaN' & date.notnull()", engine='python')
ca_single_day.head()
ca_city = ca_city.query("place=='Irvine'")
ca_city
ca_covid.confirmed_cases.describe()
ca_covid.groupby("county").confirmed_cases.describe()
ca_covid.groupby("county").confirmed_cases.describe().sort_values(by=["max"], ascending=False)
ca_county.groupby("place").confirmed_cases.describe().sort_values(by=["max"], ascending=False).head(50)
!pip install plotly
import plotly.express as px
Irvine = ca_covid.query("place == ['Irvine']")
px.bar(Irvine,
x='date',
y='confirmed_cases')
OC_Cities = ca_county.query("place == ['Santa Ana','Anaheim','Irvine']")
px.bar(OC_Cities,
x='date',
y='confirmed_cases',
color = 'place')
px.scatter(orange_single_day,
x='x',
y='y',
hover_name='place',
color='confirmed_cases')
px.scatter(ca_single_day,
x='x',
y='y',
hover_name='place',
color='confirmed_cases')
px.scatter(ca_single_day,
x='x',
y='y',
color='confirmed_cases',
size='confirmed_cases',
size_max=40,
hover_name='place',
title = 'Confirmed Cases for ' + lastdate)
px.scatter(ca_single_day,
x='x',
y='y',
color='confirmed_cases',
size='confirmed_cases',
size_max=40,
hover_name='place',
color_continuous_scale = 'RdYlGn_r') # added _r to reverse color scheme
ca_single_day_mean = ca_single_day.confirmed_cases.mean()
ca_single_day_mean
px.scatter(ca_single_day,
x='x',
y='y',
color='confirmed_cases',
size='confirmed_cases',
size_max=40,
hover_name='place',
color_continuous_scale = 'RdYlGn_r', # added _r to reverse color scheme
range_color = (0,ca_single_day_mean * 2) # double the mean
)
orange_mean = ca_county.confirmed_cases.mean()
orange_mean
px.scatter(ca_county,
x='x',
y='y',
color='confirmed_cases',
size='confirmed_cases',
size_max=40,
hover_name='place',
animation_frame='date', # this creates a frame by frame animation by day
color_continuous_scale = 'RdYlGn_r',
range_color = (0,orange_mean*2))
fig = px.scatter_geo(ca_single_day,
lon='x',
lat='y',
color='confirmed_cases',
size='confirmed_cases',
size_max=40,
hover_name='place',
scope='usa',
color_continuous_scale = 'RdYlGn_r',
range_color = (0,ca_single_day_mean * 2) # double the mean
)
fig.update_geos(fitbounds="locations")
fig = px.scatter_geo(ca_county,
lon='x',
lat='y',
color='confirmed_cases',
size='confirmed_cases',
size_max=40,
hover_name='place',
scope='usa',
animation_frame='date',
color_continuous_scale = 'RdYlGn_r',
range_color = (0,orange_mean*2))
fig.update_geos(fitbounds="locations")
# Set the map up
map_orange = folium.Map(location=[33.6846, -117.8265],
tiles = "Stamen Toner",
zoom_start = 9)
# Simple marker
folium.Marker([33.6846, -117.8265],
popup='Irvine',
icon=folium.Icon(color='green')
).add_to(map_orange)
# Circle marker
folium.CircleMarker([33.6846, -117.8265],
radius=30,
popup='Irvine',
color='red',
).add_to(map_orange)
# Interactive marker
map_orange.add_child(folium.ClickForMarker(popup="Irvine"))
map_orange
locations = ca_county[['x', 'y']]
locationlist = locations.values.tolist()
len(locationlist)
locationlist[7]
# Create a Map instance
map = folium.Map(location=[34,-118],
zoom_start=8,
control_scale=True
)
map
# Create a Map instance with different basemap
map = folium.Map(location=[34,-118],
zoom_start=8,
control_scale=True,
tiles='CartoDB dark_matter',
attr= '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>')
map
# add a circle
my_circle = folium.Circle(
radius=10000, # this is in meters
location=[34,-118],
color='crimson',
fill=True,
)
my_circle.add_to(map)
map
orange_single_day.plot(x ='x', y='y', kind = 'scatter')
orange_single_day = orange_single_day.query("confirmed_cases != 'NaN' & x < 0 & x != 'NaN' & date.notnull()", engine='python')
orange_single_day.head()
# loop through the rows in Los Angeles, and create a circle based on confirmed cases
for index, row in orange_single_day.iterrows():
# set up the variables
lat = row['y']
lon = row['x']
label = str(row['confirmed_cases']) + ' confirmed cases in ' + row['place']
size = row['confirmed_cases']
# create a circle for every row
circle = folium.Circle(
radius=size,
location=[lat,lon],
tooltip = label,
color='crimson',
fill = True
)
circle.add_to(map)
# show the map
map
import altair as alt
# reset the map (only way to get rid of circles)
map = folium.Map(location=[34,-118],
zoom_start=8,
control_scale=True,
tiles='CartoDB dark_matter',
attr= '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>')
# create a function to create circles, and also add a chart in the popup window (a lot here, will break it up later)
def createCircle(lat,lon,size,place,label):
# create a bar chart for each circle
bar = alt.Chart(ca_county.query('place == @place')).mark_bar().encode(
x=alt.X('date', axis=alt.Axis(labels=False)), # turn the labels off because there are too many
y='confirmed_cases',
color='confirmed_cases',
tooltip = ['date','place','confirmed_cases']
).properties(width=400,height=200)
# add the bar chart as a folium feature
vega = folium.features.VegaLite(
bar,
width=600,
height=200,
)
# create the circle
circle = folium.Circle(
radius=size,
location=[lat,lon],
tooltip = label,
color='crimson',
fill = True
)
# create a popup
popup = folium.Popup()
# add the chart to the popup
vega.add_to(popup)
# add the popup to the circle
popup.add_to(circle)
# add the circle to the map
circle.add_to(map)
# loop through the rows in Los Angeles, and create a circle based on confirmed cases
for index, row in orange_single_day.iterrows():
label = str(row['confirmed_cases']) + ' confirmed cases in ' + row['place']
createCircle(row['y'],row['x'],row['confirmed_cases'],row['place'],label)
# show the map
map
# save it!
map.save('index.html')
orange_single_day.head()
orange_single_day.tail()
style.use('ggplot')
ax = plt.gca()
orange_single_day.plot(kind='line',x='place',y='confirmed_cases', color='green', ax=ax, figsize=(20,10))
plt.title('Confirmed Cases in Orange County')
plt.xlabel('Place')
plt.ylabel('Count')
plt.show()
ca_county = ca_county.query("confirmed_cases != 'NaN' & x < 0 & x != 'NaN' & date.notnull()", engine='python')
ca_county.head()